<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>e车险保</title>
  <link rel="stylesheet" href="css/main.css">
  <link rel="stylesheet" href="css/reset.css">
  <link rel="stylesheet" href="css/login.css">
</head>
<body>
  <div class="login-box">
  </div>
  <div class="login">
    <h1>e车险保后台管理系统</h1>
    <form id="my-app">
      <div class="input-box"><input type="text" placeholder="手机号" id="username" v-model="formData.mobile" required><i class="username"></i><br></div>
      <div class="input-box"><input type="password" placeholder="密码" id="password" v-model="formData.password" required><i class="password"></i><br></div>
      <input type="checkbox" id="remeber" v-model="checked"><label for="remeber">记住密码</label><br>
      <input type="button" value="登录" @click="toLogin">
    </form>
  </div>
  <div class="my-alert"></div>
</body>
</html>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/vue.min.js"></script>
<script src="js/common.js"></script>
<script>
  $(function () {
      var cookies = document.cookie;
      var mobile = "",password = "",checked = false;
      var cookie_json = {};
      if(cookies){
          cookies = cookies.split(";");
      }
      for(var i = 0;i < cookies.length;i ++){
          var cookie = cookies[i];
          cookie = cookie.split("=");
          cookie_json[cookie[0]] = cookie[1];
      }
      if(cookie_json.mobile && cookie_json.password){
          mobile = cookie_json.mobile;
          password = cookie_json.password;
          checked = true;
      }
      var vm = new Vue({
          el:"#my-app",
          data:{
              formData:{
                  mobile:mobile,
                  password:password
              },
              checked:checked
          },
          methods:{
              toLogin:function(){
                  var url = ip + "userlogin";
                  if(!mobileReg.test(vm.formData.mobile)){
                      my_alert("手机号格式不正确!");
                      return;
                  }
                  if(vm.formData.password == ""){
                      my_alert("密码不能为空!");
                      return;
                  }
                  $.ajax({
                      url:url,
                      dataType:"json",
                      contentType:'application/json; charset=utf-8',
                      data:JSON.stringify(vm.formData),
                      type:"post",
                      success:function(data){
                          console.log(data);
                          if(data.code == "0000"){
                              if(vm.checked){
                                  document.cookie="mobile="+vm.formData.mobile;
                                  document.cookie="password="+vm.formData.password;
                              }
                              window.location.href = "main.html";
                          }else{
                              my_alert(data.msg);
                          }
                      },
                      error:my_error
                  });
              }
          }
      });
  });
</script>
